package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealDishService setmealDishService;

    @PostMapping
    public R<String> saveWithDish(@RequestBody SetmealDto setmealDto){

        setmealService.saveWithDish(setmealDto);

        return R.success("新增成功啦!!!");

    }
    @GetMapping("/page")
    public R<Page> pageR(int page,int pageSize,String name){

        Page<Setmeal> setmealPage=new Page<>(page,pageSize);
        Page<SetmealDto> setmealDtoPage=new Page<>();
        LambdaQueryWrapper<Setmeal> lqw=new LambdaQueryWrapper<>();
        lqw.like(name!=null,Setmeal::getName,name);
        lqw.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(setmealPage,lqw);
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");

        List<Setmeal> records = setmealPage.getRecords();
        List<SetmealDto> list = records.stream().map((item)->{
            SetmealDto dtos=new SetmealDto();
            BeanUtils.copyProperties(item,dtos);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (null!=category){
                String categoryName = category.getName();
                dtos.setCategoryName(categoryName);
            }
            return dtos;
        }).collect(Collectors.toList());

        setmealDtoPage.setRecords(list);

        return R.success(setmealDtoPage);
    }

    @DeleteMapping
    public R<String> stringR(@RequestParam List<Long> ids){
        log.info("看看都有些什么呢:{}",ids);
        setmealService.deleteWithDish(ids);
        return R.success("删除成功咯!!!!!");
    }

    @GetMapping("/list")
    public R<List<Setmeal>> dtoR(Setmeal setmeal){

        LambdaQueryWrapper<Setmeal> lqw=new LambdaQueryWrapper<>();
        lqw.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        lqw.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        List<Setmeal> list = setmealService.list(lqw);
        return R.success(list);
    }

    @PostMapping("/status/{status}")
    public R<String> Rstatus(@PathVariable int status,@RequestParam List<Long> ids){

        ids.forEach((id)->{
            LambdaQueryWrapper<Setmeal> lqw=new LambdaQueryWrapper<>();
            lqw.eq(Setmeal::getId,id);
            Setmeal one = setmealService.getOne(lqw);
            if (one!=null){
                one.setStatus(status);
            }
            setmealService.updateById(one);
        });

        return R.success("ok");
    }

    @GetMapping("/{id}")
    public R<Setmeal> Rhhs(@PathVariable Long id){
        log.info("TTTTTTTTTTTTTTTTTTTT:{}",id);
        Setmeal setmeal = setmealService.getById(id);

        return R.success(setmeal);
    }

}
